package com.dhecp.project.logstics.service.impl;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import com.dhecp.project.business.domain.LogisticsMmodeCk;
import com.dhecp.project.logstics.mapper.LogsticsMmodeCkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.common.utils.RandomValueUtils;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.base.domain.BaseInfo;
import com.dhecp.project.base.service.BaseInfoService;
import com.dhecp.project.business.domain.LogisticsMmode;
import com.dhecp.project.business.domain.LogisticsMmodeDetails;
import com.dhecp.project.business.domain.LogisticsMmodeGrads;
import com.dhecp.project.common.service.impl.BaseModuleServiceImpl;
import com.dhecp.project.logstics.mapper.LogsticsMmodeDetailsMapper;
import com.dhecp.project.logstics.mapper.LogsticsMmodeGradsMapper;
import com.dhecp.project.logstics.mapper.LogsticsMmodeMapper;
import com.dhecp.project.logstics.service.LogsticsMmodeService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 物流方式 服务层
 * @Author fxz
 * @Date 2023/03/23
 */
@Service
public class LogsticsMmodeServicelmpl extends BaseModuleServiceImpl implements LogsticsMmodeService {


    @Autowired
    private LogsticsMmodeMapper logsticsMmodeMapper;

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

    @Autowired
    private LogsticsMmodeDetailsMapper logisticsMmodeDetailsMapper;

    @Autowired
    private LogsticsMmodeGradsMapper logisticsMmodeGradsMapper;

    @Autowired
    private BaseInfoService baseInfoService;

    @Autowired
    private LogsticsMmodeCkMapper logsticsMmodeCkMapper;


    /**
     * 查询物流名称
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public List<LogisticsMmode> queryList(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句

        if (params.get("field") == null || params.get("field") == ""){
            String orderBy = "t1.createTime asc";
            PageUtils.startPageOrder(orderBy);
        }else{
            PageUtils.startPage();
        }

        return logsticsMmodeMapper.findLogisticsMmode(params);
    }


    /**
     * 新增物流方式
     * @param params
     * @return
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public AjaxResult insertLogisticsMmodeServer(Map<String, Object> params) {
        if (StringUtils.isEmpty(params)) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }

        LogisticsMmode entity = JSON.parseObject(params.get("infoMap").toString(), LogisticsMmode.class);
        int count = logsticsMmodeMapper.countByNameLogsticsWay(entity);
        if (count != 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("物流方式重复,请核实再操作！");
        }
        if (entity.getCoefficientWeight().equals("0")){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("抛重系数不能为0,请核实再操作！");
        }
        if (entity.getPriceSingleBoxReqZd().compareTo(BigDecimal.ZERO) == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("单箱最低采购价要求(￥)：不能为0,请核实再操作！");
        }
        if (entity.getPriceSingleBoxReq() == null){
            entity.setPriceSingleBoxReq(BigDecimal.ZERO);
        }
        if (entity.getPriceSingleBoxReqZd() == null){
            entity.setPriceSingleBoxReqZd(BigDecimal.ZERO);
        }
        entity.setGuid(RandomValueUtils.getGUID());
        entity.setCreateUser(ShiroUtils.getUserId());
        entity.setCreateTime(new Date());
        entity.setUpdateUser(ShiroUtils.getUserId());
        entity.setUpdateTime(new Date());
        if (entity.getGuid() == null || "".equals(entity.getGuid())){
            entity.setGuid(RandomValueUtils.getGUID());
        }

        if (entity.getNameLogsticsWay() == null || entity.getNameLogsticsWay().length() == 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }


        //新增物流方式明细
        List<LogisticsMmodeDetails> onSonData = JSON.parseArray(params.get("infoItems").toString(), LogisticsMmodeDetails.class);
        List<LogisticsMmodeDetails> logisticsMmodeDetailsList = new ArrayList<>();
        for (LogisticsMmodeDetails logisticsMmodeDetailNew : onSonData){
            logisticsMmodeDetailNew.setGuid(RandomValueUtils.getGUID());
            logisticsMmodeDetailNew.setLogisticsMmodeGuid(entity.getGuid());
            logisticsMmodeDetailNew.setCreateUser(ShiroUtils.getUserId());
            logisticsMmodeDetailNew.setCreateTime(new Date());
            logisticsMmodeDetailNew.setDeleted(false);
//            logisticsMmodeDetailsMapper.insertLogisticsMmodeDetails(logisticsMmodeDetailNew);
            logisticsMmodeDetailsList.add(logisticsMmodeDetailNew);
        }

        //判断梯度列表是否正确
        List<LogisticsMmodeGrads> grads = JSON.parseArray(params.get("grads").toString(), LogisticsMmodeGrads.class);
        List<LogisticsMmodeGrads> gradsList;
        List<LogisticsMmodeGrads> gradsList1;
        for (LogisticsMmodeGrads logisticsMmodeGrads : grads){
            if (logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads.getEndSection()) >= 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("开始值不允许小于等于结束值");
            }
            gradsList = grads.stream().filter(logisticsMmodeGrads1 -> logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads1.getStartSection()) >= 0
                        && logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads1.getEndSection()) <= 0
                            && !logisticsMmodeGrads.getGuid().equals(logisticsMmodeGrads1.getGuid())
                    ).collect(Collectors.toList());
            gradsList1 = grads.stream().filter(logisticsMmodeGrads1 -> logisticsMmodeGrads.getEndSection().compareTo(logisticsMmodeGrads1.getStartSection()) >= 0
                    && logisticsMmodeGrads.getEndSection().compareTo(logisticsMmodeGrads1.getEndSection()) <= 0
                    && !logisticsMmodeGrads.getGuid().equals(logisticsMmodeGrads1.getGuid())
            ).collect(Collectors.toList());
            if (gradsList.size() != 0 || gradsList1.size() != 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("区间有重叠");
            }
        }
        List<LogisticsMmodeGrads> logisticsMmodeGradsList = new ArrayList<>();
        for (LogisticsMmodeGrads logisticsMmodeGrads : grads){
            logisticsMmodeGrads.setGuid(RandomValueUtils.getGUID());
            logisticsMmodeGrads.setLogisticsMmodeGuid(entity.getGuid());
            logisticsMmodeGrads.setType("WLFS");
//            logisticsMmodeGradsMapper.insertLogisticsMmodeGrads(logisticsMmodeGrads);
            logisticsMmodeGradsList.add(logisticsMmodeGrads);
        }

        //判断计重明细列表是否正确
        List<LogisticsMmodeGrads> infoItemsZj = JSON.parseArray(params.get("infoItemsZj").toString(), LogisticsMmodeGrads.class);
        List<LogisticsMmodeGrads> infoItemsList;
        List<LogisticsMmodeGrads> infoItemsList1;
        for (LogisticsMmodeGrads logisticsMmodeGrads : infoItemsZj){
            if (logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads.getEndSection()) >= 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("开始值不允许小于等于结束值");
            }
            infoItemsList = infoItemsZj.stream().filter(logisticsMmodeGrads1 -> logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads1.getStartSection()) >= 0
                    && logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads1.getEndSection()) <= 0
                    && !logisticsMmodeGrads.getGuid().equals(logisticsMmodeGrads1.getGuid())
            ).collect(Collectors.toList());
            infoItemsList1 = infoItemsZj.stream().filter(logisticsMmodeGrads1 -> logisticsMmodeGrads.getEndSection().compareTo(logisticsMmodeGrads1.getStartSection()) >= 0
                    && logisticsMmodeGrads.getEndSection().compareTo(logisticsMmodeGrads1.getEndSection()) <= 0
                    && !logisticsMmodeGrads.getGuid().equals(logisticsMmodeGrads1.getGuid())
            ).collect(Collectors.toList());
            if (infoItemsList.size() != 0 || infoItemsList1.size() != 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("区间有重叠");
            }
        }

        List<LogisticsMmodeGrads> logisticsMmodeInfoItemsList = new ArrayList<>();
        for (LogisticsMmodeGrads logisticsMmodeGrads : infoItemsZj){
            logisticsMmodeGrads.setGuid(RandomValueUtils.getGUID());
            logisticsMmodeGrads.setLogisticsMmodeGuid(entity.getGuid());
            logisticsMmodeGrads.setType("WLFSTDJZ");
            logisticsMmodeInfoItemsList.add(logisticsMmodeGrads);
        }

        //物流方式仓库梯度
        List<LogisticsMmodeCk> infoItemsCk = JSON.parseArray(params.get("infoItemsCk").toString(), LogisticsMmodeCk.class);
        Set<String> warehouseNameSet = new HashSet<>();
        Set<String> warehouseNameSetNew;
        for (LogisticsMmodeCk logisticsMmodeCk : infoItemsCk){
                warehouseNameSetNew = warehouseNameSet.stream().filter(ietm -> ietm.equals(logisticsMmodeCk.getWarehouseName())
            ).collect(Collectors.toSet());
            if (warehouseNameSetNew.size() != 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("仓库名称有重复,请核实后再保存!");
            }
            warehouseNameSet.add(logisticsMmodeCk.getWarehouseName());
        }

        List<LogisticsMmodeCk> logisticsMmodeCkList = new ArrayList<>();
        for (LogisticsMmodeCk logisticsMmodeCk : infoItemsCk){
            logisticsMmodeCk.setGuid(RandomValueUtils.getGUID());
            logisticsMmodeCk.setLogisticsMmodeGuid(entity.getGuid());
            logisticsMmodeCk.setCreateUser(ShiroUtils.getUserId());
            logisticsMmodeCk.setCreateTime(new Date());
            logisticsMmodeCkList.add(logisticsMmodeCk);
        }


        logsticsMmodeMapper.insertLogisticsMmode(entity);

        int updateLength = logisticsMmodeDetailsList.size();
        int i = 0;
        while (updateLength > 100) {
            logisticsMmodeDetailsMapper.insertLogisticsMmodeDetailsBatch(logisticsMmodeDetailsList.subList(i, i + 100));

            i = i + 100;
            updateLength = updateLength - 100;
        }
        if (updateLength > 0) {
            logisticsMmodeDetailsMapper.insertLogisticsMmodeDetailsBatch(logisticsMmodeDetailsList.subList(i, i + updateLength));
        }

        logisticsMmodeGradsMapper.insertLogisticsMmodeGradsBatch(logisticsMmodeGradsList);

        logisticsMmodeGradsMapper.insertLogisticsMmodeGradsBatch(logisticsMmodeInfoItemsList);

        logsticsMmodeCkMapper.insertLogisticsMmodeCkBatch(logisticsMmodeCkList);

        return AjaxResult.success(MessageUtils.message("module.success.issuccess"),entity.getGuid());
    }


    /**
     * @Description: 查询指定单nid的首记录
     * @Param: [params]
     * @Author: fxz
     * @date: 2022-09-22 19:00
     */
    @Override
    public LogisticsMmode queryFirstByGuid(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句
        return logsticsMmodeMapper.queryFirstByGuid(params);
    }


    /**
     * 修改物流方式
     * @author fxz
     * @date: 2022-09-22
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public AjaxResult updateLogisticsMmode(Map<String, Object> params) {
        if (StringUtils.isEmpty(params)) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }

        LogisticsMmode entity = JSON.parseObject(params.get("infoMap").toString(), LogisticsMmode.class);
        int count = logsticsMmodeMapper.countByNameLogsticsWay(entity);
        if (count != 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("物流方式重复,请核实再操作！");
        }
        if (entity.getCoefficientWeight().equals("0")){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("抛重系数不能为0,请核实再操作！");
        }
        if (entity.getPriceSingleBoxReqZd().compareTo(BigDecimal.ZERO) == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("单箱最低采购价要求(￥)：不能为0,请核实再操作！");
        }
        if (entity.getPriceSingleBoxReq() == null){
            entity.setPriceSingleBoxReq(BigDecimal.ZERO);
        }
        entity.setUpdateUser(ShiroUtils.getUserId());
        entity.setUpdateTime(new Date());

        if (entity.getNameLogsticsWay() == null || entity.getNameLogsticsWay().length() == 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }


        //判断梯度列表是否正确
        List<LogisticsMmodeGrads> grads = JSON.parseArray(params.get("grads").toString(), LogisticsMmodeGrads.class);
        List<LogisticsMmodeGrads> gradsList;
        List<LogisticsMmodeGrads> gradsList1;
        for (LogisticsMmodeGrads logisticsMmodeGrads : grads){
            if (logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads.getEndSection()) >= 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("开始值不允许小于等于结束值");
            }
            gradsList = grads.stream().filter(logisticsMmodeGrads1 -> logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads1.getStartSection()) >= 0
                    && logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads1.getEndSection()) <= 0
                    && !logisticsMmodeGrads.getGuid().equals(logisticsMmodeGrads1.getGuid())
            ).collect(Collectors.toList());
            gradsList1 = grads.stream().filter(logisticsMmodeGrads1 -> logisticsMmodeGrads.getEndSection().compareTo(logisticsMmodeGrads1.getStartSection()) >= 0
                    && logisticsMmodeGrads.getEndSection().compareTo(logisticsMmodeGrads1.getEndSection()) <= 0
                    && !logisticsMmodeGrads.getGuid().equals(logisticsMmodeGrads1.getGuid())
            ).collect(Collectors.toList());
            if (gradsList.size() != 0 || gradsList1.size() != 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("区间有重叠");
            }
        }

        //判断计重明细列表是否正确
        List<LogisticsMmodeGrads> infoItemsZj = JSON.parseArray(params.get("infoItemsZj").toString(), LogisticsMmodeGrads.class);
        List<LogisticsMmodeGrads> infoItemsList;
        List<LogisticsMmodeGrads> infoItemsList1;
        for (LogisticsMmodeGrads logisticsMmodeGrads : infoItemsZj){
            if (logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads.getEndSection()) >= 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("开始值不允许小于等于结束值");
            }
            infoItemsList = infoItemsZj.stream().filter(logisticsMmodeGrads1 -> logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads1.getStartSection()) >= 0
                    && logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads1.getEndSection()) <= 0
                    && !logisticsMmodeGrads.getGuid().equals(logisticsMmodeGrads1.getGuid())
            ).collect(Collectors.toList());
            infoItemsList1 = infoItemsZj.stream().filter(logisticsMmodeGrads1 -> logisticsMmodeGrads.getEndSection().compareTo(logisticsMmodeGrads1.getStartSection()) >= 0
                    && logisticsMmodeGrads.getEndSection().compareTo(logisticsMmodeGrads1.getEndSection()) <= 0
                    && !logisticsMmodeGrads.getGuid().equals(logisticsMmodeGrads1.getGuid())
            ).collect(Collectors.toList());
            if (infoItemsList.size() != 0 || infoItemsList1.size() != 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("区间有重叠");
            }
        }

        List<LogisticsMmodeGrads> logisticsMmodeInfoItemsList = new ArrayList<>();
        for (LogisticsMmodeGrads logisticsMmodeGrads : infoItemsZj){
            logisticsMmodeGrads.setGuid(RandomValueUtils.getGUID());
            logisticsMmodeGrads.setLogisticsMmodeGuid(entity.getGuid());
            logisticsMmodeGrads.setType("WLFSTDJZ");
            logisticsMmodeInfoItemsList.add(logisticsMmodeGrads);
        }

        //删除物流方式梯度和计重明细列表
        LogisticsMmodeGrads logisticsMmodeGrads2 = new LogisticsMmodeGrads();
        logisticsMmodeGrads2.setLogisticsMmodeGuid(entity.getGuid());


        List<LogisticsMmodeGrads> logisticsMmodeGradsList = new ArrayList<>();
        for (LogisticsMmodeGrads logisticsMmodeGrads : grads){
            logisticsMmodeGrads.setGuid(RandomValueUtils.getGUID());
            logisticsMmodeGrads.setLogisticsMmodeGuid(entity.getGuid());
            logisticsMmodeGrads.setType("WLFS");
//            logisticsMmodeGradsMapper.insertLogisticsMmodeGrads(logisticsMmodeGrads);
            logisticsMmodeGradsList.add(logisticsMmodeGrads);
        }


        //删除物流方式明细
        LogisticsMmodeDetails logisticsMmodeDetails = new LogisticsMmodeDetails();
        logisticsMmodeDetails.setLogisticsMmodeGuid(entity.getGuid());

        //新增物流方式明细
        List<LogisticsMmodeDetails> onSonData = JSON.parseArray(params.get("infoItems").toString(), LogisticsMmodeDetails.class);
        List<LogisticsMmodeDetails> logisticsMmodeDetailsList = new ArrayList<>();
        for (LogisticsMmodeDetails logisticsMmodeDetailNew : onSonData){
            logisticsMmodeDetailNew.setGuid(RandomValueUtils.getGUID());
            logisticsMmodeDetailNew.setLogisticsMmodeGuid(entity.getGuid());
            logisticsMmodeDetailNew.setCreateUser(ShiroUtils.getUserId());
            logisticsMmodeDetailNew.setCreateTime(new Date());
            logisticsMmodeDetailNew.setDeleted(false);
//            logisticsMmodeDetailsMapper.insertLogisticsMmodeDetails(logisticsMmodeDetailNew);
            logisticsMmodeDetailsList.add(logisticsMmodeDetailNew);
        }

        //删除物流方式仓库梯度
        LogisticsMmodeCk logisticsMmodeCks = new LogisticsMmodeCk();
        logisticsMmodeCks.setLogisticsMmodeGuid(entity.getGuid());

        //物流方式仓库梯度
        List<LogisticsMmodeCk> infoItemsCk = JSON.parseArray(params.get("infoItemsCk").toString(), LogisticsMmodeCk.class);
        Set<String> warehouseNameSet = new HashSet<>();
        Set<String> warehouseNameSetNew;
        for (LogisticsMmodeCk logisticsMmodeCk : infoItemsCk){
            warehouseNameSetNew = warehouseNameSet.stream().filter(ietm -> ietm.equals(logisticsMmodeCk.getWarehouseName())
            ).collect(Collectors.toSet());
            if (warehouseNameSetNew.size() != 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("仓库名称有重复,请核实后再保存!");
            }
            warehouseNameSet.add(logisticsMmodeCk.getWarehouseName());
        }

        List<LogisticsMmodeCk> logisticsMmodeCkList = new ArrayList<>();
        for (LogisticsMmodeCk logisticsMmodeCk : infoItemsCk){
            logisticsMmodeCk.setGuid(RandomValueUtils.getGUID());
            logisticsMmodeCk.setLogisticsMmodeGuid(entity.getGuid());
            logisticsMmodeCk.setCreateUser(ShiroUtils.getUserId());
            logisticsMmodeCk.setCreateTime(new Date());
            logisticsMmodeCkList.add(logisticsMmodeCk);
        }

        logsticsMmodeMapper.updateByGuid(entity);

        logisticsMmodeGradsMapper.deleteByLogisticsMmodeGuid(logisticsMmodeGrads2);

        logisticsMmodeGradsMapper.insertLogisticsMmodeGradsBatch(logisticsMmodeGradsList);

        logisticsMmodeDetailsMapper.deleteByLogisticsMmodeGuid(logisticsMmodeDetails);

        int updateLength = logisticsMmodeDetailsList.size();
        int i = 0;
        while (updateLength > 100) {
            logisticsMmodeDetailsMapper.insertLogisticsMmodeDetailsBatch(logisticsMmodeDetailsList.subList(i, i + 100));

            i = i + 100;
            updateLength = updateLength - 100;
        }
        if (updateLength > 0) {
            logisticsMmodeDetailsMapper.insertLogisticsMmodeDetailsBatch(logisticsMmodeDetailsList.subList(i, i + updateLength));
        }

        logisticsMmodeGradsMapper.insertLogisticsMmodeGradsBatch(logisticsMmodeInfoItemsList);

        logsticsMmodeCkMapper.deleteByLogisticsMmodeGuid(logisticsMmodeCks);

        logsticsMmodeCkMapper.insertLogisticsMmodeCkBatch(logisticsMmodeCkList);

        return AjaxResult.success(MessageUtils.message("module.success.issuccess"),entity.getGuid());

    }

    /**
     * 删除物流方式
     * @author fxz
     * @date: 2023-03-24
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public AjaxResult deleteLogisticsMmode(Map<String, Object> params) {
        List<LogisticsMmode> logisticsMmodeList = JSON.parseArray(params.get("infoItems").toString(), LogisticsMmode.class);
        if (logisticsMmodeList == null || logisticsMmodeList.size() == 0) {
            return AjaxResult.error(MessageUtils.message("module.error.jsontoentyerror"));
        }
        for (LogisticsMmode logisticsMmode : logisticsMmodeList) {
            logsticsMmodeMapper.deleteByGuid(logisticsMmode);

            //删除物流方式明细
            LogisticsMmodeDetails logisticsMmodeDetails = new LogisticsMmodeDetails();
            logisticsMmodeDetails.setLogisticsMmodeGuid(logisticsMmode.getGuid());
            logisticsMmodeDetailsMapper.deleteByLogisticsMmodeGuid(logisticsMmodeDetails);

            //删除物流方式梯度
            LogisticsMmodeGrads logisticsMmodeGrads2 = new LogisticsMmodeGrads();
            logisticsMmodeGrads2.setLogisticsMmodeGuid(logisticsMmode.getGuid());
            logisticsMmodeGradsMapper.deleteByLogisticsMmodeGuid(logisticsMmodeGrads2);
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }

    /**
     * 同步材质
     * @author fxz
     * @date: 2023-03-24
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public AjaxResult hotMaterial(Map<String, Object> params) throws Exception {
        List<LogisticsMmode> logisticsMmodeList = JSON.parseArray(params.get("infoItems").toString(), LogisticsMmode.class);
        if (logisticsMmodeList == null || logisticsMmodeList.size() == 0) {
            return AjaxResult.error(MessageUtils.message("module.error.jsontoentyerror"));
        }
        Map<String, Object> param1 = new HashMap<String, Object>();
        Map<String, Object> param2 = new HashMap<String, Object>();
        List<LogisticsMmodeDetails> logisticsMmodeDetails1;
        List<LogisticsMmodeDetails> logisticsMmodeDetailsNew;
        for (LogisticsMmode logisticsMmode : logisticsMmodeList) {
            param1.put("logisticsMmodeGuid",logisticsMmode.getGuid());
            //查询当前的物流方式详情
            logisticsMmodeDetails1 = logisticsMmodeDetailsMapper.findLogisticsMmodeDetails(param1);

            param2.put("vtype", "spcz");
            param2.put("moduleTable", "TB_baseinfo_spcz");
            //查询当前材质
            List<BaseInfo> list  = baseInfoService.queryBaseInfoList(param2);
            List<LogisticsMmodeDetails> logisticsMmodeDetailsList = new ArrayList<>();
            LogisticsMmodeDetails logisticsMmodeDetails;
            for (BaseInfo baseInfo : list){
                logisticsMmodeDetailsNew =
                        logisticsMmodeDetails1.stream().filter(logisticsMmodeDetail -> logisticsMmodeDetail.getMaterialName().equals(baseInfo.getVname()))
                                .collect(Collectors.toList());


                if (baseInfo.getBdeletedtext().equals("否")) {
                    logisticsMmodeDetails = new LogisticsMmodeDetails();
                    if (logisticsMmodeDetailsNew.size() == 0) {
                        //新增
                        logisticsMmodeDetails.setGuid(RandomValueUtils.getGUID());
                        logisticsMmodeDetails.setLogisticsMmodeGuid(logisticsMmode.getGuid());
                        logisticsMmodeDetails.setMaterialName(baseInfo.getVname());
                        logisticsMmodeDetails.setMaterialCost(BigDecimal.ZERO);
                        logisticsMmodeDetails.setDeleted(false);
                        logisticsMmodeDetails.setCreateTime(new Date());
                        logisticsMmodeDetails.setCreateUser(ShiroUtils.getUserId());
                        logisticsMmodeDetailsList.add(logisticsMmodeDetails);
                    }
                }
            }
            //需要删除的
            List<String> baseInfoDel = new ArrayList<>();
            List<BaseInfo> baseInfoList = new ArrayList<>();
            for (LogisticsMmodeDetails logisticsMmodeDetails2 : logisticsMmodeDetails1){
                baseInfoList =
                        list.stream().filter(baseInfo1 -> logisticsMmodeDetails2.getMaterialName().equals(baseInfo1.getVname()))
                                .collect(Collectors.toList());
                if (baseInfoList.size() == 0){
                    baseInfoDel.add(logisticsMmodeDetails2.getMaterialName());
                }
            }
            //根据物流方式guid批量删除
            Map<String, Object> deleteMap = new HashMap<>();
            int deleteLength = baseInfoDel.size();
            int j = 0;
            while (deleteLength > 1000) {
                deleteMap.put("logisticsMmodeGuids",logisticsMmode.getGuid());
                deleteMap.put("materialName", baseInfoDel.subList(j, j + 1000));
                logisticsMmodeDetailsMapper.deleteLogisticsMmodeDetailsMap(deleteMap);
                j = j + 1000;
                deleteLength = deleteLength - 1000;
            }
            if (deleteLength > 0) {
                deleteMap.put("logisticsMmodeGuid",logisticsMmode.getGuid());
                deleteMap.put("materialName", baseInfoDel.subList(j, j + deleteLength));
                logisticsMmodeDetailsMapper.deleteLogisticsMmodeDetailsMap(deleteMap);
            }
            //批量新增
            int insertLength = logisticsMmodeDetailsList.size();
            int i = 0;
            while (insertLength > 90) {
                logisticsMmodeDetailsMapper.insertLogisticsMmodeDetailsBatch(logisticsMmodeDetailsList.subList(i, i + 90));
                i = i + 90;
                insertLength = insertLength - 90;
            }
            if (insertLength > 0) {
                logisticsMmodeDetailsMapper.insertLogisticsMmodeDetailsBatch(logisticsMmodeDetailsList.subList(i, i + insertLength));
            }
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }
}
